﻿package com.athena.process
{
	import flash.display.MovieClip;
	import flash.display.*;
	import flash.utils.*;
	import flash.text.TextFormat;
	import flash.events.*;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.net.URLRequest ;
	import flash.net.URLVariables ;
	import flash.net.URLRequestMethod ;
	import flash.net.URLRequestHeader ;	
	import flash.net.URLLoaderDataFormat ;
	import com.adobe.serialization.json.JSON;
	/**
	 * ...
	 * @author ...
	 */
	public class ProfileFunction 
	{
		public static var obj : Object ;
		public static function Show_Popup_Profile() : void {
			if ((Cocaro.mainStage_mc.currentFrame != 3) && (Cocaro.mainStage_mc.currentFrame != 4)) return ;
			var mc : MovieClip = MovieClip(new Popup_Profile()) ;
			mc.Close_btn.addEventListener(MouseEvent.CLICK, Close_click);
			mc.OK_btn.addEventListener(MouseEvent.CLICK, OK_click);
			var tfStyle:TextFormat = new TextFormat();
			tfStyle.size = 12;
			tfStyle.color = 0xFFFFFF;
			tfStyle.font = "Arial";
			mc.Name_txt.setStyle("textFormat", tfStyle);
			mc.Id_txt.setStyle("textFormat", tfStyle);
			mc.Mobile_txt.setStyle("textFormat", tfStyle);
			mc.Job_txt.setStyle("textFormat", tfStyle);
			mc.Address_txt.setStyle("textFormat", tfStyle);
			mc.Email_txt.setStyle("textFormat", tfStyle);
			mc.name = "Profile" ;
			mc.x = 0 ;
			mc.y = 0 ;
			if (Cocaro.ObjProfile == null) {
				Cocaro.mainStage_mc.addChild(mc) ;	
				LoadProfile() ;
			} else {
				if (Cocaro.ObjProfile.N != null) {
					mc.Name_txt.text = Cocaro.ObjProfile.N ;
					mc.Id_txt.text = Cocaro.ObjProfile.Id ;
					mc.Mobile_txt.text = Cocaro.ObjProfile.M ;
					mc.Job_txt.text = Cocaro.ObjProfile.J ;
					mc.Address_txt.text = Cocaro.ObjProfile.A ;
					mc.Email_txt.text = Cocaro.ObjProfile.E ;
				}
				Cocaro.mainStage_mc.addChild(mc) ;
			}
		}
		
		public static function Profile_Click(evt : MouseEvent) {
			Cocaro.SoundKick() ;	
			if (Cocaro.currPlayer.Lock != 0) {
				Cocaro.kickProfile = true ;
				LockFunction.FormUnLock() ;
			} else 
				Show_Popup_Profile() ;			
		}
		public static function LoadProfile() : void {
			var request:URLRequest = new URLRequest("http://services.athena.vn/ServiceGame/Profiles.aspx?uid=" + Cocaro.user.UserId);
			request.method = URLRequestMethod.POST;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.load(request);
			function onComplete (event:Event):void {
				var obj : Object = JSON.decode(event.target.data) ;
				Cocaro.ObjProfile = new Object() ;
				Cocaro.ObjProfile.N = obj.N ;
				Cocaro.ObjProfile.Id = obj.Id ;
				Cocaro.ObjProfile.M = obj.M ;
				Cocaro.ObjProfile.J = obj.J ;
				Cocaro.ObjProfile.A = obj.A ;
				Cocaro.ObjProfile.E = obj.E ;
				var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Profile")) ;
				if (mc == null) return ;
				mc.Name_txt.text = Cocaro.ObjProfile.N ;
				mc.Id_txt.text = Cocaro.ObjProfile.Id ;
				mc.Mobile_txt.text = Cocaro.ObjProfile.M ;
				mc.Job_txt.text = Cocaro.ObjProfile.J ;
				mc.Address_txt.text = Cocaro.ObjProfile.A ;
				mc.Email_txt.text = Cocaro.ObjProfile.E ;
			}
		}
		public static function OK_click(evt:MouseEvent) { //Xu ly Kick OK tren 
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Profile")) ;
			if (mc == null) return ;
			obj = new Object() ;
			obj.evt = "pf" ;
			obj.N = mc.Name_txt.text ;
			obj.Id = mc.Id_txt.text ;
			obj.M = mc.Mobile_txt.text ;
			obj.J = mc.Job_txt.text ;
			obj.A = mc.Address_txt.text ;
			obj.E = mc.Email_txt.text ;
			Cocaro.ObjProfile.N = obj.N ;
			Cocaro.ObjProfile.Id = obj.Id ;
			Cocaro.ObjProfile.M = obj.M ;
			Cocaro.ObjProfile.J = obj.J ;
			Cocaro.ObjProfile.A = obj.A ;
			Cocaro.ObjProfile.E = obj.E ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
			//Cocaro.tf.appendText("=>SendPF:" + JSON.encode(obj)) ;
		}
		public static function Profile_OK() {
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Profile")) ;
			if (mc == null) return ;
			Cocaro.mainStage_mc.removeChild(mc) ;		
			obj = new Object() ;
			BasicFunction.ShowAlert("Bạn đã khai báo thông tin cá nhân thành công với Số CMT là: " + Cocaro.ObjProfile.Id + " và Số điện thoại là: " + Cocaro.ObjProfile.M + ", Chúc bạn chơi Game vui vẻ!") ;
		}
		public static function Close_click(evt:MouseEvent) { //Xu ly Kick Close tren popup chuyen tien
			Cocaro.SoundKick() ;
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Profile")) ;
			if (mc == null) return ;
			Cocaro.mainStage_mc.removeChild(mc) ;			
		}
	}
	
}